package com.coszero.utils.device

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.coszero.utils.utils.LogX

/**
 * Desc： 电池电量工具类
 *
 * @author xmqian
 * Email:xmqian93@163.com
 * Date: 2020/12/22 0022 10:17
 */
object BatteryUtils {
    const val TAG: String = "battery"
    private var mPowerConnectionReceiver: BroadcastReceiver? = null

    /**
     * 获取当前系统电量百分比
     *
     * @param context context
     * @return 返回当前电量  单位百分比，需要自己加%号
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    fun getElectricity(context: Context): Int {
        val batterymanager: BatteryManager =
            context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
        val intProperty: Int =
            batterymanager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
        LogX.i(TAG, "当前电量为：" + intProperty + "%")
        return intProperty
    }

    /**
     * 获取电池信息
     */
    fun getElect(context: Context) {
        val filter: IntentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        val receiver: Intent? = context.registerReceiver(null, filter)
        val level: Int = receiver!!.getIntExtra("level", 0) //获取当前电量
        val scale: Int = receiver.getIntExtra("scale", 0) //获取总电量
        val status: Int = receiver.getIntExtra("status", 0) //获取充电状态
        val voltage: Int = receiver.getIntExtra("voltage", 0) //获取电压(mv)
        val temperature: Int = receiver.getIntExtra("temperature", 0) //获取温度(数值)
        val t: Double = temperature / 10.0 //运算转换,电池摄氏温度，默认获取的非摄氏温度值

        Log.e(TAG, "当前电量:" + level + "%")
        Log.e(TAG, "总电量:" + scale)
        Log.e(TAG, "充电状态:" + status)
        Log.e(TAG, "电压:" + voltage)
        Log.e(TAG, "电池温度:" + t)
    }

    /**
     * 监听电池电量状态
     *
     * @param context context
     * @param listener listener
     */
    fun registerBatteryState(context: Context?, listener: BatteryStateListener) {
        mPowerConnectionReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val status: Int = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
                val mIsCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                        status == BatteryManager.BATTERY_STATUS_FULL
                //当前电量
                val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
                //总电量
                val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)

                listener.state((level.toFloat()) / scale, mIsCharging)
            }
        }
    }

    /**
     * @param context 取消电池监听
     */
    fun unregisterBatteryState(context: Context) {
        if (mPowerConnectionReceiver != null) {
            context.unregisterReceiver(mPowerConnectionReceiver)
        }
    }

    interface BatteryStateListener {
        /**
         * @param electricity 当前电量比例
         * @param isCharging 是否在充电 true 是
         */
        fun state(electricity: Float, isCharging: Boolean)
    }
}
